package com.wy.io;

import java.io.*;
import java.util.Scanner;

/**
 * @author 美女
 * @date 2022/05/13 15:46
 **/
public class ReplaceTool {
    public static void main(String[] args) throws IOException {
        try(InputStream is=new FileInputStream("src.txt")){//从src中读
            try(Scanner scanner=new Scanner(is,"utf-8")){//按utf-8读取到内容
                //改后的值需要放入这个新文件中
                try(OutputStream os=new FileOutputStream("dest.txt")){
                    try(Writer writer=new OutputStreamWriter(os,"UTF-8")){
                        try(PrintWriter printWriter=new PrintWriter(writer)){
                            while(scanner.hasNextLine()){//只要scanner还有下一行
                                String line= scanner.nextLine();//就一直读
                                //读到数据，开始进行替换
                                String replace=line.replace("你好","我好");//字符串是不可变的，所以得到一个新的字符串才有意义，光调用没有用
                                //把新的字符串repalce再写到某个新的文件中即可，这里是dest
                                //调用printerWriter.println()
                                printWriter.println(replace);
                            }
                            //循环完后刷新，保证都被写进去dest了，缓存区没有东西遗留
                            printWriter.flush();
                        }
                    }
                }
            }
        }
    }
}
